package com.huluobo.a2201mvvm_imdemo.mvvm;

import android.app.ProgressDialog;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2201mvvm_imdemo.net.NetUtils;

/**
 * Created by LC on 2024/3/22.
 */
public abstract class BaseMVVMActivity<VDB extends ViewDataBinding, VM extends BaseViewModel> extends AppCompatActivity {
    protected VDB dataBinding;
    protected VM viewModel;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //1.绑定视图
        dataBinding = DataBindingUtil.setContentView(this, getLayoutId());

        //2.实例化ViewMode
        viewModel = new ViewModelProvider(this).get(getViewModel());

        initView();
        initData();
    }

    protected abstract void initData();

    protected abstract void initView();

    protected abstract Class<VM> getViewModel();

    protected abstract int getLayoutId();

    public void showProgressDialog(String message) {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(message);
        progressDialog.show();
    }

    public void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!NetUtils.getInstance(this).isConnected()) {
            ToastUtils.showLong("当前没有网络连接,请检查网络");
        }

        if (NetUtils.getInstance(this).isSlowlyNetwork()) {
            ToastUtils.showLong("当前网络有些慢,请检查网络连接");
        }
    }
}
